package com.wy.tingshu.servlet.album;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.mysql.cj.protocol.x.ConfinedInputStream;
import com.wy.tingshu.service.AlbumService;
import com.wy.tingshu.util.DBUtil;
import com.wy.tingshu.util.Log;
import com.wy.tingshu.view_object.ListAlbumView;
import com.wy.tingshu.view_object.ResultAlbumView;
import com.wy.tingshu.view_object.StudioTrackView;
import com.wy.tingshu.view_object.UserVO;
import lombok.SneakyThrows;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 * 专辑列表
 **/
@WebServlet("/studio/album/list.json")
public class ListJsonServlet extends HttpServlet{
    private final AlbumService albumService=new AlbumService();
    private final ObjectMapper objectMapper=new ObjectMapper();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Log.println("访问GET /studio/album/list.json");
        ResultAlbumView resultView=new ResultAlbumView();//存放用户信息和要展示的专辑信息
        //1.获取当前登录用户
        HttpSession session=req.getSession(false);
        UserVO currentUser=null;
        if(session!=null){
            currentUser=(UserVO) session.getAttribute("currentUser");

        }
        //2.如果用户未登录  else  用户已登录
        if(currentUser==null){
            Log.println("用户未登录");
            resultView.currentUser=null;
            resultView.albumList=null;//不写出来也都是null

        }else{
            Log.println("用户已登录："+currentUser);
            resultView.currentUser=currentUser;
            //得到当前用户的所有专辑列表List<view_object.ListAlbumView> list
            resultView.albumList=albumService.listOfUser(currentUser);
        }
        Log.println("准备序列化的对象是："+resultView);
        //使用Jackson提供代码，将list对象转化成json格式的字符串类型
        String json=objectMapper.writeValueAsString(resultView);
        Log.println("序列化后的结果是："+json);
        //响应该字符串
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("application/json");
        PrintWriter writer=resp.getWriter();
        Log.println("将json写入响应体中");
        writer.println(json);
    }
}
//public class ListJsonServlet extends HttpServlet {
//    private static class ResultView{//用户和他的专辑列表存储在albumList中
//        public UserVO currentUser;
//        public List<AlbumView> albumList=new ArrayList<>();
//    }
//    private static class AlbumView{
//        public Integer aid;
//        public String title;
//        public String cover;
//        public String state;
//
//        public AlbumView(Integer aid, String title, String cover, int state) {
//            this.aid = aid;
//            this.title = title;
//            this.cover = cover;
//            switch (state){
//                case 0: this.state="已下线";break;
//                case 1:this.state="未发布";break;
//                case 2:this.state="已发布";break;
//                default:this.state="未知";break;
//            }
//        }
//    }
//
//    @SneakyThrows
//    @Override
//    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//
//        //1.获取当前登录用户
//        HttpSession session=req.getSession(false);
//        if(session==null){
//            throw new  RuntimeException("未登录");
//
//        }
//        UserVO currentUser=(UserVO) session.getAttribute("currentUser");
//        if(currentUser==null){
//            throw new  RuntimeException("未登录");
//        }
//        ResultView resultView=new ResultView();
//        resultView.currentUser=currentUser;
//        //2.执行SQL+3.组装成需要的格式(组合出来的数据就是resultView)
//        try (Connection c= DBUtil.connection()){
//            //通过uid展示当前用户的专辑列表
//            String sql="select aid,title,cover,state from albums where uid=? order by aid desc";
//            try (PreparedStatement ps=c.prepareStatement(sql)){
//                ps.setInt(1,currentUser.uid);
//                try(ResultSet rs= ps.executeQuery()){
//                    while (rs.next()){
//                        AlbumView view=new AlbumView(
//                                rs.getInt("aid"),
//                                rs.getString("title"),
//                                rs.getString("cover"),
//                                rs.getInt("state")
//                        );
//                        resultView.albumList.add(view);
//                    }
//                }
//            }
//        }
//        //4.转JSON
//        ObjectMapper objectMapper=new ObjectMapper();
//        String json = objectMapper.writeValueAsString(resultView);
//        //5.响应
//        resp.setCharacterEncoding("utf-8");
//        resp.setContentType("application/json");
//        resp.getWriter().println(json);
//    }
//}
